<?php
/**
 * Class Controllerindex
 * 首页
 * @author gary
 */
class ControllerUser extends Router
{
    protected $uncheckfunc = array('login');

    /**
     * 初始化模板
     */
    public function Init()
    {
        Services_User::factory()->checkLogin();
        $func = !empty($this->url[2]) ? $this->url[2] : '';
        if (empty($this->reg['id']) && !FendArray::isInArray($func, $this->uncheckfunc, 0)) {
            FendHttp::doBreak('/');
            return;
        }
    }

    /**
     * 获取当前用户的输入的进销存信息
     */
    public function getList()
    {
        $tmy = array();
        $uid = $this->reg['id'];
        $start = (int)FendHttp::doGet('start');
        $psize = (int)FendHttp::doGet('psize');
        $psize = empty($psize) ? 0 : $psize;
        $start = empty($start) ? 0 : $start;

        $name       = FendHttp::doRequest('name');
        $bianhao    = FendHttp::doRequest('bianhao');
        $tiaoma     = FendHttp::doRequest('tiaoma');
        $pinming    = FendHttp::doRequest('pinming');
        $huohao     = FendHttp::doRequest('huohao');
        $riqi       = FendHttp::doRequest('riqi');
        $uid        = $this->reg['id'];
        if (!empty($name)) {
            $where = empty($where) ? "  name ='%{$name}%'" : " {$where} OR name ='%{$name}%'";
        }
        if (!empty($bianhao)) {
            $where = empty($where) ? " bianhao ='%{$bianhao}%'" : " {$where} OR  bianhao ='%{$bianhao}%'";
        }
        if (!empty($tiaoma)) {
            $where = empty($where) ? " tiaoma ='%{$tiaoma}%' " : " {$where} OR tiaoma ='%{$tiaoma}%'";
        }
        if (!empty($pinming)) {
            $where = empty($where) ? " pinming ='%{$pinming}%' " : " {$where} OR pinming ='%{$pinming}%'";
        }
        if (!empty($huohao)) {
            $where = empty($where) ? " huohao ='%{$huohao}%'" : " {$where} OR huohao ='%{$huohao}%'";
        }
        if (!empty($riqi)) {
            $where = empty($where) ? " riqi ='{$riqi}'" : " {$where} OR huohao ='{$riqi}'";
        }
        $where = !empty($where) ? " ({$where}) AND uid={$uid}" : " uid={$uid}";

        $read = Fend_Read::Factory('ht_user_huizong');
        $read->setLimit($start, $psize);
        $read->setWhere($where);
        $sql = $read->getSql();
        $query = $read->query($sql);
        while ($rs = $read->fetch($query)) {
            $tmy['list'][] = $rs;
        }
        $tmy['total'] = $read->getSum();
        self::showMsg($tmy, 1);
    }

    /**
     * 获取单条进销存信息
     */
    public function getInfo()
    {
        $name = FendHttp::doRequest('name');
        $bianhao = FendHttp::doRequest('bianhao');
        $tiaoma = FendHttp::doRequest('tiaoma');
        $pinming = FendHttp::doRequest('pinming');
        $huohao = FendHttp::doRequest('huohao');
        $uid = $this->reg['id'];
        if (!empty($name)) {
            $where = empty($where) ? "  name ='%{$name}%'" : " {$where} OR name ='%{$name}%'";
        }
        if (!empty($bianhao)) {
            $where = empty($where) ? " bianhao ='%{$bianhao}%'" : " {$where} OR  bianhao ='%{$bianhao}%'";
        }
        if (!empty($tiaoma)) {
            $where = empty($where) ? " tiaoma ='%{$tiaoma}%' " : " {$where} OR tiaoma ='%{$tiaoma}%'";
        }
        if (!empty($pinming)) {
            $where = empty($where) ? " pinming ='%{$pinming}%' " : " {$where} OR pinming ='%{$pinming}%'";
        }
        if (!empty($huohao)) {
            $where = empty($where) ? " huohao ='%{$huohao}%'" : " {$where} OR huohao ='%{$huohao}%'";
        }
        $where = !empty($where) ? " ({$where}) AND uid={$uid}" : " uid={$uid}";
        $read = Fend_Read::Factory('ht_user_huizong');
        $read->setWhere($where);
        $read->setOrder(" ctime DESC");
        $sql = $read->getSql();
        $tmy = $read->get($sql);
        self::showMsg($tmy, 1);
    }

    /**
     * 获取用户的信息
     */
    public function getUserInfo()
    {
        $uid    = (int)FendHttp::doRequest('uid');
        $read   = Fend_Read::Factory('ucm_sysuser');
        $where  = " id={$uid}";
        $filed  = array('id','name','nickname','phone','islock','fix','pid');
        $tmy    = $read->getInfoByCondition($where,$filed);
        //锁定用户权限
        if(empty($this->reg['fix']) && $tmy['pid']!=$this->reg['id'] && $this->reg['id']!=$uid){
            return self::showMsg("你无权查看此用户信息",-1);
        }
        return self::showMsg($tmy,1);
    }

    /**
     * 退出登陆
     */
    public function loginout()
    {
        if(!empty($_COOKIE['ht'])){
            $authkey = $_COOKIE['ht'];
            FendHttp::setCookie('ht','',-1,'/','.baokuwang.com');
            Fend_Cache::factory(1)->del('user_'.$authkey);
        }
        return self::showMsg("退出成功",1);
    }
    /**
     * 添加进销存
     */
    public function addShop()
    {
        $msg['name']    = '商品名字';
        $msg['bianhao'] = '编号';
        $msg['tiaoma']  = '条码';
        $msg['pinming'] = '品名';
        $msg['riqi']    = '日期';
        $msg['huohao']  = '货号';
        $msg['zuocun']  = '昨存';
        $msg['xiaohuo'] = '销货';
        $msg['jinhuo']  = '进货';
        $msg['fanchang']= '返厂';
        $msg['ketui']   = '客退';
        $msg['cunhuo']  = '存货';
        $msg['tiaoru']  = '调入';
        $msg['tiaochu'] = '调出';

        $rs['name']     = FendHttp::doRequest('name');
        $rs['bianhao']  = FendHttp::doRequest('bianhao');
        $rs['tiaoma']   = FendHttp::doRequest('tiaoma');
        $rs['pinming']  = FendHttp::doRequest('pinming');
        $rs['riqi']     = FendHttp::doRequest('riqi');
        $rs['huohao']   = FendHttp::doRequest('huohao');
        $rs['zuocun']   = FendHttp::doRequest('zuocun');
        $rs['xiaohuo']  = FendHttp::doRequest('xiaohuo');
        $rs['jinhuo']   = FendHttp::doRequest('jinhuo');
        $rs['fanchang'] = FendHttp::doRequest('fanchang');
        $rs['ketui']    = FendHttp::doRequest('ketui');
        $rs['cunhuo']   = FendHttp::doRequest('cunhuo');
        $rs['tiaoru']   = FendHttp::doRequest('tiaoru');
        $rs['tiaochu']  = FendHttp::doRequest('tiaochu');

        foreach ($rs as $key => $val) {
            if (empty($val)) {
                return self::showMsg("请输入" . $msg[$key], -1);
            }
        }
        $rs['uid'] = $this->reg['id'];
        $rs['uname'] = $this->reg['name'];
        $write = Fend_Write::Factory('ht_user_huizong');
        $where = "bianhao='{$rs['bianhao']}' and tiaoma='{$rs['tiaoma']}' and riqi='{$rs['riqi']}'";
        $repinfo = $write->getInfoByCondition($where, array('id'));
        if (empty($repinfo)) {
            $rs['ctime'] = time();
            $sql = $write->subSQL($rs, 'ht_user_huizong', 'insert');
            $write->query($sql);
            $rs['id'] = $write->getLastId();
            if (!empty($rs['id'])) {
                return self::showMsg("操作成功", 1);
            } else {
                return self::showMsg("操作失败", -1);
            }
        } else {
            $rs['utime'] = time();
            $rs['euid'] = $this->reg['id'];
            $rs['euname'] = $this->reg['name'];
            $sql = $write->subSQL($rs, 'ht_user_huizong', 'update', " id={$repinfo['id']}");
            if ($write->query($sql)) {
                return self::showMsg("操作成功", 1);
            } else {
                return self::showMsg("操作失败", -1);
            }
        }

    }

    /**
     * 获取用户列表
     */
    public function getUserList()
    {
        $tmy = array();
        $uid = $this->reg['id'];
        //超级管理员查看所有,非超级管理员查看自己及其子管理员
        $where = " (id={$uid} OR pid={$uid})";
        if (!empty($this->reg['fix'])) {
            $where = '1';
        }
        $start = (int)FendHttp::doRequest('start');
        $psize = (int)FendHttp::doRequest('psize');
        $name = FendHttp::doRequest('uname');
        if (!empty($name)) {
            $where .= " and name like '%{$name}%'";
        }

        $phone = FendHttp::doRequest('phone');
        if (!empty($phone)) {
            $where .= " and phone like '%{$phone}%'";
        }

        $nickname = FendHttp::doRequest('nickname');
        if (!empty($nickname)) {
            $where .= " and nickname like '%{$nickname}%'";
        }

        $psize = empty($psize) ? 0 : $psize;
        $start = empty($start) ? 0 : $start;
        $read = Fend_Read::Factory('ucm_sysuser');
        $read->setLimit($start, $psize);
        $read->setWhere($where);
        $read->setField(array('id','name','phone','nickname','utime','ctime','islock','fix'));
        $sql = $read->getSql();
        $query = $read->query($sql);
        while ($rs = $read->fetch($query)) {
            $rs['utime_name'] = !empty($rs['utime'])?date('Y年m月d日 H时i分',$rs['utime']):'';
            $rs['ctime_name'] = !empty($rs['ctime'])?date('Y年m月d日 H时i分',$rs['ctime']):'';
            $rs['lock_name']  = !empty($rs['islock'])?'锁定中':'激活中';
            $rs['fix_name']   = !empty($rs['fix'])?'管理员':'普通用户';
            $tmy['list'][] = $rs;
        }
        $tmy['total'] = $read->getSum();
        self::showMsg($tmy, 1);

    }

    /**
     * 新增用户
     */
    public function addUser()
    {

        $msg['name']        = '账户名';
        $msg['phone']       = '手机号';
        $msg['nickname']    = '真实姓名';
        $msg['pwd']         = '用户密码';
        $id                 = FendHttp::doRequest('uid');
        $rs['name']         = FendHttp::doRequest('uname');
        $rs['phone']        = FendHttp::doRequest('phone');
        $rs['nickname']     = FendHttp::doRequest('nickname');
        $rs['pwd']          = FendHttp::doRequest('pwd');
        foreach ($rs as $key => $val) {
            if (empty($val)) {
                return self::showMsg("请输入" . $msg[$key], -1);
            }
        }

        $write = Fend_Write::Factory('ucm_sysuser');

        //判断是否是要修改账户
        if(!empty($id)){

            $filed = array('id','pid','name','phone','nickname');
            $uinfo = $write->getListByCondition(array('id'=>$id),$filed);
            if(empty($uinfo)){
                return self::showMsg("你修改的账户不存在",-1);
            }
            /*
             * 判断当前用户是否有权限修改账户信息
             * 当前登录用户!=被修改用户
             * 并且当前登录用户既不是管理员也不是当前被修改用户的父级管理员
             */
            if($this->reg['id']!=$id && empty($this->reg['fix']) && $uinfo['pid']!=$this->reg['id']){
                return self::showMsg("你无权修改此账户信息",-1);
            }

            //验证用户名
            if($uinfo['name']!=$rs['name']){
                $where = "name='{$rs['name']}'";
                $repname = $write->getInfoByCondition($where);
                if(!empty($repname)){
                    return self::showMsg("此账户已存在,不能修改为此账户名",-1);
                }
            }
            //验证电话号码
            if($uinfo['phone'] != $rs['phone']){
                $where =" phone='{$rs['phone']}'";
                $repname = $write->getInfoByCondition($where);
                if(!empty($repname)){
                    return self::showMsg("此店电话已被使用",-1);
                }
            }
            //验证真实姓名
            if($uinfo['nickname'] != $rs['nickname']){
                $where ="nickname='{$rs['nickname']}'";
                $repname = $write->getInfoByCondition($where);
                if(!empty($repname)){
                    return self::showMsg("此姓名已存在",-1);
                }
            }
            $rs['utime'] = time();
            $sql = $write->subSQL($rs,'ucm_sysuser','update'," id='{$id}'");
            if($write->query($sql)){
                return self::showMsg("修改成功",1);
            }else{
                return self::showMsg("修改失败",-1);
            }

        }else{
            $rs['pid'] = $this->reg['id'];
            $where = " name='{$rs['name']}'";
            $repname = $write->getInfoByCondition($where);
            if(!empty($repname)){
                return self::showMsg("此账户已存在",-1);
            }
            $where =" phone='{$rs['phone']}'";
            $repname = $write->getInfoByCondition($where);
            if(!empty($repname)){
                return self::showMsg("此电话已被使用",-1);
            }
            $where=" nickname='{$rs['nickname']}'";
            $repname = $write->getInfoByCondition($where);
            if(!empty($repname)){
                return self::showMsg("此姓名已存在",-1);
            }
            $rs['ctime'] = time();
            $sql = $write->subSQL($rs,'ucm_sysuser','insert');
            $write->query($sql);
            $id = $write->getLastId();
            if(!empty($id)){
                return self::showMsg("添加成功",1);
            }else{
                return self::showMsg("添加失败",-1);
            }
        }

    }

    /**
     * 锁定账户
     */
    public function lockUser()
    {
        $id = (int)FendHttp::doRequest('uid');
        if(empty($id)){
            return self::showMsg("参数异常",-1);
        }
        $write = Fend_Write::Factory('ucm_sysuser');
        $uinfo = $write->getInfoByCondition(array('id'=>$id),array('id','islock','pid'));
        if(empty($uinfo)){
            return self::showMsg("当前用户不存在",-1);
        }
        //锁定用户权限
        if(empty($this->reg['fix']) && $uinfo['pid']!=$this->reg['id']){
            return self::showMsg("你无权锁定此账户",-1);
        }
        if($this->reg['id']==$id){
            return self::showMsg("自己无法锁定自己",-1);
        }
        $rs['utime'] = time();
        $rs['islock']= !empty($uinfo['islock'])?0:1;
        $sql = $write->subSQL($rs,'ucm_sysuser','update'," id={$id}");
        if($write->query($sql)){
            return self::showMsg(!empty($uinfo['islock'])?'解锁成功':'锁定成功',1);
        }else{
            return self::showMsg("操作失败",-1);
        }

    }


    /**
     * 主讲老师登陆入口
     *
     *  根据用户名、密码，进行登录
     *
     * @return string 直播列表
     */
    public function login()
    {
        $tmy = array();
        if(empty($this->reg['id'])) {
            $return = self::dologin();
            if($return!==true){
                self::showMsg($return,-1);
                return;
            }
        }
        self::showMsg('登陆成功',1);
    }

    protected  function dologin()
    {
        $username = FendHttp::doRequest('uname');
        $password = FendHttp::doRequest('pwd');
        if(empty($username) || empty($password)){
            return array('code'=>0,'msg'=>'用户名或密码错误');
        }
        $pwd    = md5($password);
        $read   = Fend_Read::Factory('ucm_sysuser');
        $field  = array('id','name','islock','fix','nickname','phone','pid');
        $rs     = $read->getInfoByCondition(array('name'=>$username,'pwd'=>$pwd),$field);
        if(empty($rs)){
            return array('code'=>0,'msg'=>'用户不存在');
        }
        if(!empty($rs['islock'])){
            return array('code'=>0,'msg'=>'账户被锁定');
        }
        $authkey = md5($username.$pwd);
        FendHttp::setCookie('ht',$authkey,time(),'/','.baokuwang.com');
        Fend_Cache::factory(1)->set('user_'.$authkey,$rs);
        return true;

    }




}